package com.learner.course.controller.admin;

import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.course.service.VideoService;
import com.learner.pojo.course.CourseMedia;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ice2020x
 * @Date: 2023/3/8
 * @Description: 视频控制器
 **/
@RestController
@RequestMapping("/admin/edu/video")
@Slf4j
public class VideoController {
    @Autowired
    private VideoService videoService;

    /**
     * 保存一条视频信息
     *
     * @param courseMedia 媒体信息
     * @return 结果
     */
    @PostMapping("save")
    public CourseMedia save(@RequestBody CourseMedia courseMedia) {
        return videoService.save(courseMedia);
    }

    /**
     * 根据id获取一条记录
     *
     * @param id id
     * @return 结果
     */
    @GetMapping("get/{id}")
    public CourseMedia getById(@PathVariable("id") String id) {
        return videoService.getById(id);
    }

    /**
     * 远程删除vod的视频
     *
     * @param id 媒体id
     */
    @DeleteMapping("remove/{id}")
    public void removeById(@PathVariable("id") String id) {
        videoService.removeMediaVideoById(id);
        boolean result = videoService.removeById(id);
        if (!result) {
            throw ExceptionUtil.createException(RestException.class, 2000004);
        }
    }
}
